<?php

/**
 * @file
 * Definition of example_handler_custom_field
 */

/**
 * Provides a custom views field.
 */
class jessica_stylebook_handler_style_item_listing_buy_link extends views_handler_field {
    function option_definition() {
        $options = parent::option_definition();
        return $options;
    }

    function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
    }

    function query() {
        // do nothing -- to override the parent query.
    }

    function render($data) {
        //dpm($data, 'data');
        $items = (!empty($data->field_field_item_buy_link)) ? $data->field_field_item_buy_link : null;
        $link = '';

        if (count($items) > 0) {
            foreach ($items as $key => $item) {
                //dpm($item, 'item');

                if ($item['raw']['safe_value']) {
                    $link .= '<a class="field-item-buy" href="' . $item['raw']['safe_value'] . '">';

                    $shop =  $data->field_field_item_shop_name;

                    if (!empty($shop)) {
                        $link .= 'Buy at ' . $shop[0]['raw']['safe_value'];
                    } else {
                        $link .= 'Buy';
                    }

                    $link .= '</a>';
                }
            }
        } else {
            $path = base_path() . drupal_get_path_alias('node/'.$data->nid);

            return '<a class="field-item-view" href="' . $path . '">View detail</a>';

        }

        return $link;
    }
}

